﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using tf2hudmaker.Properties;

namespace tf2hudmaker.Forms
{
    public partial class form_setdirectory : Form
    {
        private Thread SearchingProcess;
        private string startfolder;
        private bool searching;

        public form_setdirectory()
        {
            InitializeComponent();

            this.label_path.Text = Settings.Default._tf2path;
            this.label_pathsearch.Text = "";
            this.label_pathsearch.Enabled = false;
            this.label_currentsearch.Enabled = false;
            this.label_verified.Text = "Verified: " + Settings.Default._tf2pathverified;

            this.searching = false;

            this.folderbrowser_manually.Description = "Select the folder containing the \"hl2.exe\" file, usually located in \nProgram Files (x86)\\Steam\\SteamApps\\common\\Team Fortress 2";
            this.folderbrowser_search.Description = "Select the folder where the research should start.";
            this.startfolder = "";
        }

        private void button_verify_Click(object sender, EventArgs e)
        {
            bool valid = DirectoryManager.CheckTf2Directory(Settings.Default._tf2path);

            string good = "The current TF2 game path is correct.";
            string error = "The current TF2 game path is not a valid one\n(can't find hl2.exe). Use the tools just below to set a new one.";

            if (valid)
                MessageBox.Show(good, "Valid Path", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            else
                MessageBox.Show(error, "Invalid Path", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void button_manually_Click(object sender, EventArgs e)
        {
            this.folderbrowser_manually.ShowDialog();
            
            string path = this.folderbrowser_manually.SelectedPath;
            bool valid = DirectoryManager.CheckTf2Directory(path);

            string good = "The specified TF2 game path is correct and as been set.";
            string error = "The specified TF2 game path is not a valid one\n(can't find hl2.exe). Choose another folder.";

            if (valid)
            {
                MessageBox.Show(good, "Valid Path", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                Settings.Default._tf2path = path;
                Settings.Default._tf2pathverified = true;
                UserSettings.SaveSettings();
                this.label_path.Text = Settings.Default._tf2path;
                this.label_verified.Text = "Verified: " + Settings.Default._tf2pathverified;
            }
            else
                MessageBox.Show(error, "Invalid Path", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void BeginSearch()
        {
            this.button_manually.Enabled = false;
            this.button_searchfrom.Enabled = false;
            this.button_searchall.Enabled = false;
            this.button_verify.Enabled = false;

            this.label_currentsearch.Enabled = true;
            this.label_pathsearch.Enabled = true;

            this.searching = true;

            SearchingProcess = new Thread(new ThreadStart(Search));
            SearchingProcess.Start();

            timer_updater.Enabled = true;
        }

        private void EndSearch()
        {
            this.button_manually.Enabled = true;
            this.button_searchfrom.Enabled = true;
            this.button_searchall.Enabled = true;
            this.button_verify.Enabled = true;

            this.label_currentsearch.Enabled = false;
            this.label_pathsearch.Enabled = false;

            timer_updater.Enabled = false;
        }

        private void Search()
        {
            bool success = DirectoryManager.GetTf2Directory(this.startfolder);

            string good = "Game path has been found and saved.";
            string bad = "Not found, please search from another folder or verify your\nTeam Fortress 2 installation.";

            if (success)
            {
                MessageBox.Show(good, "Game Folder Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                Settings.Default._tf2pathverified = true;
                UserSettings.SaveSettings();
            }
            else
                MessageBox.Show(bad, "Game Folder Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            this.searching = false;
        }

        private void timer_updater_Tick(object sender, EventArgs e)
        {
            this.label_pathsearch.Text = DirectoryManager.CurrentSearch;
            if (!this.searching)
            {
                this.label_path.Text = Settings.Default._tf2path;
                this.label_verified.Text = "Verified: " + Settings.Default._tf2pathverified;
                EndSearch();
            }
        }

        private void button_searchall_Click(object sender, EventArgs e)
        {
            this.startfolder = "";
            BeginSearch();
        }

        private void button_searchfrom_Click(object sender, EventArgs e)
        {
            this.folderbrowser_search.ShowDialog();
            string path = this.folderbrowser_search.SelectedPath;

            this.startfolder = path;
            BeginSearch();
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
